'use strict';

const Service = require('egg').Service;
var moment = require('moment');

const {
    ERROR,
    SUCCESS,
} = require('../util/util');

class CommentService extends Service {
  async selectComments(
    tid, 
    limit, 
    offset
  ) {
      const { ctx, logger } = this

      try {
          // 检查tid是否存在
          const topic = await ctx.model.Topic.findOne({
            where: {
                tid: tid/1
            }
          })

          if (!topic || topic.state !== 1) {
            return Object.assign(ERROR(), {
                msg: '帖子不存在',
            });
          }

          // 帖子正常查出相关的评论
          const comments = await ctx.model.Comment.findAndCountAll({
              include :[{
                model: ctx.model.User,
                as: 'puser',
                attributes: [ 'uid', 'username', 'avatarUrl' ],
              },{
                model: ctx.model.User,
                as: 'ruser',
                attributes: [ 'uid', 'username', 'avatarUrl' ],
              }],
              where: {
                tid: tid/1
              },
              order: [['created_at', 'ASC']],
              limit: limit,
              offset: offset
          })

          return Object.assign(SUCCESS(), {
            data: Object.assign(comments),
          });
      } catch (error) {
        logger.error(error)
        return Object.assign(ERROR(), {
            msg: '服务器错误',
        });
      }
  }

  async addComment(
    tid,
    rid,
    puid,
    ruid,
    comment
  ) {
      const { ctx, logger } = this

      try {
          // 查询tid是否存在

          const topic = await ctx.model.Topic.findOne({
              where: {
                  tid: tid/1
              }
          })

          if (!topic || topic.state !== 1) {
            return Object.assign(ERROR(), {
                msg: '评论失败, 帖子不存在',
            });
          }
          
          const result = await ctx.model.Comment.create({
            tid,
            rid,
            puid,
            ruid,
            comment,
          })

          // 评论成功之后添加一条msg
          if ( puid/1 !== ruid/1 ) {
            const msg = await ctx.service.message.creatMessage(tid, puid, ruid, result.id, rid, comment)
          }

          // 帖子正常查出相关的评论
          const comments = await ctx.model.Comment.findAndCountAll({
              include :[{
                model: ctx.model.User,
                as: 'puser',
                attributes: [ 'uid', 'username', 'avatarUrl' ],
              },{
                model: ctx.model.User,
                as: 'ruser',
                attributes: [ 'uid', 'username', 'avatarUrl' ],
              }],
              where: {
                tid: tid/1
              },
              order: [['created_at', 'ASC']]
          }) 

          return Object.assign(SUCCESS(), {
            msg: '评论成功',
            data: Object.assign(comments)
          });
      } catch (error) {
        logger.error(error)
        return Object.assign(ERROR(), {
            msg: '服务器错误',
        });
      }
  }
}

module.exports = CommentService;
